local BattleCard = require("View/UIBattle/SubPanel/BattleCard")
local BattleSkillPanel = require("View/UIBattle/SubPanel/BattleSkillPanel")
---@class UIBattle:UIBase
UIBattle = UIBase.New(PanelNames.UIBattle)

function UIBattle:OnCreate()
    ---@type BattleCard[]
    self.cardItemList = {}
    self.updateTimer = nil
    for i = 0, 4 do
        local card = self:GetChild("Btn_Card0" .. i)
        table.insert(self.cardItemList, self:AddComponent(BattleCard, card))
    end
    ---@type Battle.Entity[]
    self.skillHeroList = {}--技能列表里的actor
    self.textTime = self:GetChild("Text_Time")
    self.btnClickScene = self:GetChild("Btn_ClickScene")
    local bar = self:GetChild("Bar_MyHp")
    ---@type FairyGUI.GProgressBar
    self.myHpBar = bar
    --self.myHpBarText = bar:GetChild("hp")
    bar = self:GetChild("Bar_EnemyHp")
    ---@type FairyGUI.GProgressBar
    self.enemyHpBar = bar
    --self.enemyHpBarText = bar:GetChild("hp")
    ---@type FairyGUI.GProgressBar
    bar = self:GetChild("Com_EnergyBar")
    self.energyBar = bar:GetChild("bar")
    self.energyBarText = bar:GetChild("text_energyNum")
    ---@type FairyGUI.GProgressBar
    self.actionBar = self:GetChild("Bar_Action")
    self.accelerate = self:GetChild("Com_Accelerate")
    self.warning = self:GetChild("Img_Warning")
    self.weatherCtrl = self:GetController("isWeather")
    self.weatherText = self:GetChild("Text_Weather")

    self.cacheGraphList = {}
    self.effGraphList = {}
    self:RegisterTouchBegin(self.btnClickScene, self.TouchSceneBegin)
    self:RegisterTouchMove(self.btnClickScene, self.TouchSceneMove)
    self:RegisterTouchEnd(self.btnClickScene, self.TouchSceneEnd)

    self:RegisterBattleEvent()

    self:InitHandCard()
    self:BattleTimeUpdate()
    self:EnergyUpdate()
    self:BattleActionCountUpdate()
    self:ActorCreate(Battle.ActorManager.GetLord(Battle.CampType.Blue))
    self:ActorCreate(Battle.ActorManager.GetLord(Battle.CampType.Red))


    self:AddComponent(BattleSkillPanel,self:GetChild("Com_BattleSkill"))


end
function UIBattle:OnDestroy()
    self:UnRegisterBattleEvent()

end
function UIBattle:RegisterBattleEvent()
    Battle.GlobalEventSystem.Register(Battle.Event.PlayerHpChanged, self.Event_HpChanged,self)
    Battle.GlobalEventSystem.Register(Battle.Event.ActorCreate, self.Event_ActorCreate,self)
    Battle.GlobalEventSystem.Register(Battle.Event.ActorDead, self.ActorDead,self)
    Battle.GlobalEventSystem.Register(Battle.Event.HandCardUse, self.HandCardUse,self)
    Battle.GlobalEventSystem.Register(Battle.Event.BattleTimeInit, self.BattleTimeUpdate,self)
    Battle.GlobalEventSystem.Register(Battle.Event.BattleEnergyUpdate, self.EnergyUpdate,self)
    Battle.GlobalEventSystem.Register(Battle.Event.BattleActionCountUpdate, self.BattleActionCountUpdate,self)

    Battle.GlobalEventSystem.Register(Battle.Event.BattleStageChanged,self.BattleStageChanged,self)
    Battle.GlobalEventSystem.Register(Battle.Event.AccelerateStart,self.AccelerateStart,self)
    Battle.GlobalEventSystem.Register(Battle.Event.WeatherStart,self.WeatherStart,self)

end
function UIBattle:UnRegisterBattleEvent()
    Battle.GlobalEventSystem.UnRegister(Battle.Event.PlayerHpChanged, self.Event_HpChanged,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.ActorCreate, self.Event_ActorCreate,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.ActorDead, self.ActorDead,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.HandCardUse, self.HandCardUse,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BattleTimeInit, self.BattleTimeUpdate,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BattleEnergyUpdate, self.EnergyUpdate,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BattleActionCountUpdate, self.BattleActionCountUpdate,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BattleStageChanged,self.BattleStageChanged,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.AccelerateStart,self.AccelerateStart,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.WeatherStart,self.WeatherStart,self)

end
function UIBattle:InitHandCard()
    for k, v in ipairs(self.cardItemList) do
        local cardList = Battle.PlayerSystem.GetMyPlayer().handCards
        v:SetCard(cardList[k], k)
    end
end
function UIBattle:TouchSceneBegin(evt)
    Battle.KeyMgr.TouchBegin()
end
function UIBattle:TouchSceneMove()
    Battle.KeyMgr.TouchMove()
end
function UIBattle:TouchSceneEnd(evt)
    Battle.KeyMgr.TouchEnd()
end

---@param args BattleEvent
function UIBattle:Event_ActorCreate(args)
    self:ActorCreate(args.sender)
end
---@param actor Battle.Entity
function UIBattle:ActorCreate(actor)
    self:HpChanged(actor)
end
function UIBattle:HpChanged(defend)
    if defend.actorType ~= Battle.ActorType.Lord then
        return
    end
    --我方主角
    if defend.camp == Battle.CampType.Blue then
        local hp = defend:GetHp()
        local max = defend:GetHpMax()
        self.myHpBar.value = hp
        self.myHpBar.max = max
        --self.myHpBarText.text = math.floor(hp) .. "/" .. max
    end
    --敌方主角
    if defend.camp == Battle.CampType.Red then
        local hp = defend:GetHp()
        local max = defend:GetHpMax()
        self.enemyHpBar.value =  hp
        self.enemyHpBar.max = max
        --self.enemyHpBarText.text = math.floor(hp) .. "/" .. max
    end
end
---@param args BattleEvent
function UIBattle:Event_HpChanged(args)
    self:HpChanged(args.sender)
end
function UIBattle:HandCardUse(args)
    local card = args.card
    self:InitHandCard()
end
---@param args BattleEvent
function UIBattle:ActorDead(args)

end
function UIBattle:BattleTimeUpdate()
    self:UpdateBatTime()
    if not self.updateTimer then
        self.updateTimer = self:TimeCall(self.UpdateBatTime, 1000, -1)
    end
end
function UIBattle:UpdateBatTime()
    local time = Battle.ClientBattle.countdownTime
    self.textTime.text = GetMinuteSecondStr(time)
    if time <= 0 then
        self:RemoveTimeCall(self.UpdateBatTime)
    end
end
function UIBattle:EnergyUpdate()
    local player = Battle.PlayerSystem.GetMyPlayer()
    local max = player.energyMax
    local cur = player.energy
    self.energyBarText.text = tostring(cur)
    self.energyBar.value = cur/max*100
    if cur < max then
        self.energyBar:TweenValue((cur + 1)/max*100,player.energyRecoveryTime)
    end
    for i = 1,4 do
        local c = self.cardItemList[i]
        c:SetGray(c.card.cost > cur)
    end
end
function UIBattle:BattleActionCountUpdate()
    local player = Battle.PlayerSystem.GetMyPlayer()
    local max = player.actionMax
    local cur = player.action
    self.actionBar.max = max
    self.actionBar.value = cur
    if cur < max then
        self.actionBar:TweenValue(cur + player.actionRecoveryCount,player.actionRecoveryTime)
    end

end
function UIBattle:BattleStageChanged(args)
    local stage = args.stage
    if stage == Battle.BattleStage.Result then
        showUI(PanelNames.UIBattleResult, Battle.ClientBattle.battleResult)
        closeUI(PanelNames.UIBattle)
    end
end
function UIBattle:AccelerateAnimOut()
    local anim = self.accelerate:GetTransition("out")
    anim:Play()
    self.warning.visible = false
end
function UIBattle:AccelerateStart()
    self.accelerate.visible = true
    self.warning.visible = true
    local anim = self.accelerate:GetTransition("in")
    anim:Play()
    self:GetTransition("warning"):Play()
    self:TimeCall(self.AccelerateAnimOut, Battle.GlobalConfig.GetInt(19))
end
---@param args Battle.WeatherStartArgs
function UIBattle:WeatherStart(args)
    self.weatherCtrl.selectedIndex = 1
    local cfg = DataTable.GetById("t_weather",args.weatherId)
    self.weatherText.text = cfg.f_Describe
end

function UIBattle:ShowSkillReadyEff(startX,startY,endX,endY)
    ---@type GGraph
    local graph
    if #self.cacheGraphList > 0 then
        graph =  table.remove(self.cacheGraphList)
    else
        graph = GGraph.New()
        graph:SetSize(100, 100)
        self.fguiObj:AddChild(graph)
    end
    ---@type GoWrapper
    local wrap = GoWrapper.New()
    graph:SetNativeObject(wrap)
    graph:SetXY(startX,startY)
    graph:TweenMove(Vector2(endX,endY),0.5):SetEase(FairyGUI.EaseType.Linear):OnComplete(function()
        if wrap.wrapTarget then
            Battle.PoolMgr.ReturnPrefabToPool(Battle.EFFECT_PATH.."UI_Trail_03",wrap.wrapTarget)
            wrap.wrapTarget = nil
            wrap:Dispose()
            table.insert(self.cacheGraphList,graph)
        end
    end)
    Battle.PoolMgr.LoadPrefabFromPool(Battle.EFFECT_PATH.."UI_Trail_03", function(tb,go)
        wrap.wrapTarget = go

        Util.SetLocalPosition(go,0,0,0)
    end)
    --Battle.PoolMgr.LoadPrefabFromPool(Battle.EFFECT_PATH.."UI_Trail_03", self.LoadSkillReadyEffFinished,self,wrap)

end
return UIBattle